﻿/*********************************************
 创建者：  george
 创建时间：2016-10-24
 功能说明：系统按钮的增删改查
 ********************************************/
using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Systems;
using EntityFramework.Extensions;
using george.Web;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseAPI
{
    [Description("系统按钮的增删改查")]
    public class ButtonHandler : BaseHandler
    {
        [Description("模块ID=0返回所有按钮,〉0返回页面下的按钮")]
        [ReturnType(typeof(IList<model.ModifyBtnData>))]
        public ApiResult GetList()
        {
            var dc = new SysEntities();
            var  query = (from b in dc.sys_button where b.IsRemove==false select b);
            if (!query.Any()) return Success(new model.ModifyBtnData());
            var result = query.OrderBy(t => t.Sort).ToList().Select(o => new model.ModifyBtnData
            {
                ID = o.ID,
                BtnEvent = o.BtnEvent,
                BtnID = o.BtnID,
                BtnName = o.BtnName,
                CssID = o.CssID,
                IsEffective = o.IsEffective,
                Memo = o.Memo,
                Sort = o.Sort,
                BtnType=o.BtnType,
                MoldID=o.MoldID,
                BackColor=o.BackColor,
                Icon=o.ICon
            }).ToList();
            return Success(result);
        }

        [Description("返回按钮详情")]
        [ReturnType(typeof(model.ModifyBtnData))]
        public ApiResult GetID([ParamDescription("按钮ID")]int BtnID = 0)
        {
            var dc = new SysEntities();
            var query = dc.sys_button.FirstOrDefault(t => t.IsRemove == false && t.ID==BtnID);
            if (query==null) return Success(new model.ModifyBtnData());
            var result =  new model.ModifyBtnData
            {
                ID = query.ID,
                BtnEvent = query.BtnEvent,
                BtnID = query.BtnID,
                BtnName = query.BtnName,
                CssID = query.CssID,
                IsEffective = query.IsEffective,
                Memo = query.Memo,
                Sort = query.Sort,
                BtnType = query.BtnType,
                MoldID = query.MoldID,
                BackColor=query.BackColor,
                Icon=query.ICon
            };
            return Success(result);
        }

        [JsonType]
        [Description("新建按钮信息")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Create([ParamDescription("输入参数")] model.CreateBtnData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.BtnName)) return Error("按钮名称不能为空");
            if (string.IsNullOrWhiteSpace(param.BtnID)) return Error("按钮ID不能为空");
            if (param.BtnName.Length > 12) return Error("【按钮名称】长度超出限制");
            if (param.BtnID.Length > 50) return Error("【按钮ID】长度超出限制");
            if (!string.IsNullOrWhiteSpace(param.Memo))
                if (param.Memo.Length > 50) return Error("【备注】长度超出限制");
            var dc = new SysEntities();
            if (dc.sys_button.Any(t => t.BtnName == param.BtnName && t.IsRemove==false)) return Error("按钮名称已存在");
            var btn = new sys_button
            {
                BtnEvent = param.BtnEvent,
                BtnID = param.BtnID,
                BtnName = param.BtnName,
                CssID = param.CssID,
                IsEffective =param.IsEffective,
                IsRemove = false,
                Memo = param.Memo,
                Sort = param.Sort,
                BtnType=param.BtnType,
                MoldID=param.MoldID,
                BackColor=param.BackColor,
                ICon=param.Icon
            };
            dc.sys_button.Add(btn);
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [JsonType]
        [Description("修改按钮信息")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Modify([ParamDescription("输入参数")] model.ModifyBtnData param)
        {
            if (param == null) return Error("参数对象为空");
            if (string.IsNullOrWhiteSpace(param.BtnName)) return Error("按钮名称不能为空");
            if (string.IsNullOrWhiteSpace(param.BtnID)) return Error("按钮ID不能为空");
            if (param.ID <= 0) return Error("无效的ID");
            if (param.BtnName.Length > 12) return Error("【按钮名称】长度超出限制");
            if (param.BtnID.Length > 50) return Error("【按钮ID】长度超出限制");
            if (!string.IsNullOrWhiteSpace(param.Memo))
                if (param.Memo.Length > 50) return Error("【备注】长度超出限制");
            var dc = new SysEntities();
            if (dc.sys_button.Any(t => t.BtnName == param.BtnName && t.IsRemove == false && t.ID !=param.ID)) return Error("按钮名称已存在");
            var query = dc.sys_button.FirstOrDefault(t=>t.ID==param.ID);
            if (query == null) return Error("无数据");
            query.BtnEvent = param.BtnEvent;
            query.BtnID = param.BtnID;
            query.BtnName = param.BtnName;
            query.CssID = param.CssID;
            query.IsEffective = param.IsEffective;
            query.Memo = param.Memo;
            query.Sort = param.Sort;
            query.BtnType = param.BtnType;
            query.MoldID = param.MoldID;
            query.BackColor = param.BackColor;
            query.ICon = param.Icon;
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [Description("删除按钮")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult Remove([ParamDescription("按钮ID")] int ID)
        {
            var dc = new SysEntities();
            if (dc.sys_module_button.Any(t => t.ButtonID == ID)) return Error("按钮已被引用，无法删除");
            var query = dc.sys_button.FirstOrDefault(t => t.ID == ID);
            if (query == null) return Error("无数据");
            //逻辑删除
            query.IsRemove = true;
            try
            {
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [JsonType]
        [Description("初始化模块按钮")]
        [ReturnType(typeof(int), "0－成功")]
        public ApiResult InitModuleButton([ParamDescription("输入参数")]model.InitData param)
        {
            var dc = new SysEntities();
            if (param == null || param.list.Count==0) return Error("参数为空");
            dc.sys_module_button.Delete();
            foreach (var item in param.list)
            {
                if (item.BtnList == null || item.BtnList.Count == 0) continue;
                foreach (var t in item.BtnList)
                {
                    var add = new sys_module_button
                    {
                        ButtonID = t,
                        ModuleID = item.ModuleID
                    };
                    dc.sys_module_button.Add(add);
                }
            }
            try
            {
                dc.SaveChanges();
            }catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [Description("获取模块按钮")]
        [ReturnType(typeof(IList<model.ResutData>))]
        public ApiResult GetModuleButton()
        {
            var dc = new SysEntities();
            var query = (from m in dc.sys_module_button
                         group m by m.ModuleID into g
                         select new
                         {
                             g.Key,
                             btnList = (from b in dc.sys_module_button where b.ModuleID == g.Key select new { b.ModuleID, b.ButtonID })
                         });
            var result = query.Select(t => new model.ResutData
            {
                ModuleID = t.Key,
                BtnList = t.btnList.Select(o => o.ButtonID).ToList()
            }).ToList();
            return Success(result);
        }

        [Description("获取模块下设置好的按钮列表")]
        [ReturnType(typeof(IList<model.ModifyBtnData>))]
        public ApiResult GetSetButtonList([ParamDescription("模块ID")]int moduleId = 0)
        {
            var dc = new SysEntities();
            var query = (from m in dc.sys_button
                         join mb in dc.sys_module_button on m.ID equals mb.ButtonID 
                         where m.IsEffective && m.IsRemove == false && mb.ModuleID==moduleId
                         select new
                         {
                             m,mb.ModuleID,
                             styleId=(from s in dc.sys_config where s.dictValue=="btn-Style" select s.Value).FirstOrDefault()
                         });
            var result = query.OrderBy(t => t.m.Sort).Select(t => new model.ModifyBtnData
            {
                ID = t.m.ID,
                BtnEvent = t.m.BtnEvent,
                BtnID = t.m.BtnID,
                BtnName = t.m.BtnName,
                CssID = t.m.CssID,
                IsEffective = t.m.IsEffective,
                Memo = t.m.Memo,
                Sort = t.m.Sort,
                BtnType = t.m.BtnType,
                MoldID = t.m.MoldID,
                BackColor = t.m.BackColor,
                Icon = t.m.ICon,
                Sytle=t.styleId
            }).ToList();
            return Success(result);
        }
    } 
}
